﻿using MahApps.Metro;
using ReflectionStudio.Classes;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using MahApps.Metro.Controls;
using ReflectionStudio.Core.Business;
using ReflectionStudio.Components.UserControls;
namespace WPFExcelReport
{
    public class AccentColorMenuData
    {
        public string Name { get; set; }
        public Brush BorderColorBrush { get; set; }
        public Brush ColorBrush { get; set; }

        private ICommand changeAccentCommand;

        public ICommand ChangeAccentCommand
        {
            get
            {
                return this.changeAccentCommand ??
                    (changeAccentCommand = new SimpleCommand { CanExecuteDelegate = x => true, ExecuteDelegate = x => this.DoChangeTheme(x) });
            }
        }

        protected virtual void DoChangeTheme(object sender)
        {
            var theme = ThemeManager.DetectAppStyle(Application.Current);
            var accent = ThemeManager.GetAccent(this.Name);
            ThemeManager.ChangeAppStyle(Application.Current, accent, theme.Item1);
        }
    }

    public class AppThemeMenuData : AccentColorMenuData
    {
        protected override void DoChangeTheme(object sender)
        {
            var theme = ThemeManager.DetectAppStyle(Application.Current);
            var appTheme = ThemeManager.GetAppTheme(this.Name);
            ThemeManager.ChangeAppStyle(Application.Current, theme.Item2, appTheme);
        }
    }

    public class SimpleCommand : ICommand
    {
        public Predicate<object> CanExecuteDelegate { get; set; }
        public Action<object> ExecuteDelegate { get; set; }

        public bool CanExecute(object parameter)
        {
            if (CanExecuteDelegate != null)
                return CanExecuteDelegate(parameter);
            return true; // if there is no can execute default to true
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            if (ExecuteDelegate != null)
                ExecuteDelegate(parameter);
        }
    }
    public partial class MetroViewModel : ViewModelBase
    {
        //    <ResourceDictionary Source="pack://application:,,,/ReflectionStudio.Controls;component/Resources/Controls/TabControl.xaml"/>
        //<ResourceDictionary Source="pack://application:,,,/ReflectionStudio.Controls;component/Resources/Controls/MetroChart.xaml"/>
        public VICC_MENUDEF menu;
        private DocumentFactory docs
        {
            get
            {
                return DocumentFactory.Instance;
            }
        }

        private string _LogText; public string LogText
        {
            get { return _LogText; }
            set { _LogText = value; OnPropertyChanged("LogText", value); }
        }

        private string _MenuText; public string MenuText
        {
            get { return _MenuText; }
            set { _MenuText = value; OnPropertyChanged("MenuText", value); }
        }
        private string _AppThemeText; public string AppThemeText
        {
            get { return _AppThemeText; }
            set { _AppThemeText = value; OnPropertyChanged("AppThemeText", value); }
        }
        private string _AccentColorText; public string AccentColorText
        {
            get { return _AccentColorText; }
            set { _AccentColorText = value; OnPropertyChanged("AccentColorText", value); }
        }
        private string _IntexAccount; public string IntexAccount { get { return _IntexAccount; } set { _IntexAccount = value; OnPropertyChanged("IntexAccount", value); } }

        private bool _IsIntexReportClient; public bool IsIntexReportClient { get { return _IsIntexReportClient; } set { _IsIntexReportClient = value; OnPropertyChanged("IsIntexReportClient", value); } }
        private bool _IsTST; public bool IsTST { get { return _IsTST; } set { _IsTST = value; OnPropertyChanged("IsTST", value); } }
        private bool _IsTS; public bool IsTS { get { return _IsTS; } set { _IsTS = value; OnPropertyChanged("IsTS", value); } }
        private bool _IsEN; public bool IsEN { get { return _IsEN; } set { _IsEN = value; OnPropertyChanged("IsEN", value); } }
        private bool _IsCN; public bool IsCN { get { return _IsCN; } set { _IsCN = value; OnPropertyChanged("IsCN", value); } }
        private bool _IsZM; public bool IsZM { get { return _IsZM; } set { _IsZM = value; OnPropertyChanged("IsZM", value); } }
        private string _ActiveDoc; public string ActiveDoc
        {
            get { return _ActiveDoc; }
            set { _ActiveDoc = value; OnPropertyChanged("ActiveDoc", value); }
        }

        #region --- CloseCommand ---

        private ICommand _CloseTabCommand;
        /// <summary>
        /// Returns a command that closes a TabItem.
        /// </summary>
        public ICommand CloseTabCommand
        {
            get
            {
                if (_CloseTabCommand == null)
                {
                    _CloseTabCommand = new Utils.RelayCommand(
                        param => this.CloseTab_Execute(param),
                        param => this.CloseTab_CanExecute(param)
                        );
                }
                return _CloseTabCommand;
            }
        }

        /// <summary>
        /// Called when the command is to be executed.
        /// </summary>
        /// <param name="parm">
        /// The TabItem in which the Close-button was clicked.
        /// </param>
        private void CloseTab_Execute(object parm)
        {
            MetroTabItem tb = parm as MetroTabItem;
            if (tb != null)
            {
                docs.MainTab.Items.Remove(parm);
                int idx = 0;
                bool isfound = false;
                foreach (DocumentBase doc in DocumentFactory.Instance.Documents)
                {
                    if (tb.Tag == null)
                        continue;
                    if (doc.Tag.ToString() == tb.Tag.ToString())
                    {
                        isfound = true;
                        break;
                    }
                    idx++;
                }
                if (isfound)
                {
                    docs.Documents.RemoveAt(idx);
                }
            }
        }

        /// <summary>
        /// Called when the availability of the Close command needs to be determined.
        /// </summary>
        /// <param name="parm">
        /// The TabItem for which to determine the availability of the Close-command.
        /// </param>
        private bool CloseTab_CanExecute(object parm)
        {
            //For the sample, the closing of TabItems will only be
            //unavailable for disabled TabItems and the very first TabItem.
            MetroTabItem ti = parm as MetroTabItem;
            if (ti != null && ti != docs.MainTab.Items[0])
                //We have a valid reference to a TabItem, so return 
                //true if the TabItem is enabled.
                return ti.IsEnabled;

            //If no reference to a TabItem could be obtained, the command 
            //cannot be executed
            return false;
        }

        #endregion

        private bool _IsLogOn;
        public bool IsLogOn
        {
            get { return _IsLogOn; }
            set
            {
                _IsLogOn = value;
                OnPropertyChanged("IsLogOn", value);
            }
        }
        public List<AccentColorMenuData> AccentColors { get; set; }
        public List<AppThemeMenuData> AppThemes { get; set; }

        private bool _NeedLogOn;
        public bool NeedLogOn
        {
            get { return _NeedLogOn; }
            set
            {
                _NeedLogOn = value;
                OnPropertyChanged("NeedLogOn", value);
            }
        }
        private bool _IsShowLog;
        public bool IsShowLog
        {
            get { return _IsShowLog; }
            set
            {
                _IsShowLog = value;
                OnPropertyChanged("IsShowLog", value);
            }
        }

        public MetroViewModel()
            : base()
        {
            CheckDirectory();
            menu = new VICC_MENUDEF();
            SetEnv = new DataSet();
            SetData = new DataSet();
            SetProd = new DataSet();
            IsTST = false;
            // create accent color menu items for the demo
            this.AccentColors = ThemeManager.Accents
                                            .Select(a => new AccentColorMenuData() { Name = a.Name, ColorBrush = a.Resources["AccentColorBrush"] as Brush })
                                            .ToList();

            // create metro theme color menu items for the demo
            this.AppThemes = ThemeManager.AppThemes
                                           .Select(a => new AppThemeMenuData() { Name = a.Name, BorderColorBrush = a.Resources["BlackColorBrush"] as Brush, ColorBrush = a.Resources["WhiteColorBrush"] as Brush })
                                           .ToList();
            MenuText = "菜单";
            LogText = "日志";
            AppThemeText = "基色";
            AccentColorText = "配色";
            

            switch (app.company)
            {
                case "TST":
                    app.AppMode = "TISSUE";
                    IsTS = true;
                    IsTST = true;
                    app.WAEHRSL = "RMB";
                    IsCN = true;
                    IsEN = false;
                    break;
                case "AFS":
                    app.AppMode = app.company;
                    app.WAEHRSL = "RMB";
                    MenuText = "Menu";
                    LogText = "Log";
                    AppThemeText = "AppThem";
                    AccentColorText = "AccentColor";
                    IsCN = false;
                    IsEN = true;
                    break;
                default:
                    app.AppMode = app.company;
                    app.WAEHRSL = "RMB";
                    IsCN = true;
                    IsEN = false;
                    break;
            }
        }

        public virtual void CheckDirectory()
        {
            string sql = "";
            if (!System.IO.Directory.Exists(@"C:\IntexReport"))
            {
                System.IO.Directory.CreateDirectory(@"C:\IntexReport");
            }
            if (!System.IO.Directory.Exists(@"C:\IntexReport\Data"))
            {
                System.IO.Directory.CreateDirectory(@"C:\IntexReport\Data");
            }
        }
    }
}
